﻿#include "CLoadMap.h"
#include "Factory.h"

CLoadMap::CLoadMap(string fileGameObject, string fileQuadtree)
{
	_fileGameObject = fileGameObject;
	_fileQuadtree = fileQuadtree;
	LoadGameObject();
}


CLoadMap::CLoadMap(string fileGameObject, string fileQuadtree, bool isFromSaved)
{
	_fileGameObject = fileGameObject;
	_fileQuadtree = fileQuadtree;
	if(isFromSaved==true)
		LoadGameObjectFromSaveFile();	
	else
		LoadGameObject();
}


void CLoadMap::LoadGameObject()
{
	//vector<GameObject*> listGameObject;
	ifstream fileStream(_fileGameObject);
	int heigh, width,id, typeObject;
	float x, y;
	EObjectType type = EL_Brick;
	// read file
	fileStream>>width;
	mapWidth = width;
	if ( fileStream.is_open())
	{
		while ( !fileStream.eof() )
		{
			fileStream>>id;
			fileStream>>typeObject;
			fileStream>>x;
			fileStream>>y;
			switch (typeObject)
			{
			case 101:
				type = EL_Brick;
				break;
			case 102:
				type = EL_Block;
				break;
			case 103:
				type = EL_Land1;
				break;
			case 104:
				type = EL_Land2;
				break;
			case 105:
				type = EL_Land3;
				break;
			case 106:
				type = EL_PiPe;
				break;
			case 107:
				type = EL_BarDown;
				break;
			case 108:
				type = EL_BarUp;
				break;
			case 109:
				type = EPipe_Down;
				break;
			case 110:
				type = EPipe_Up;
				break;
			case 111:
				type = EPipe_Right;
				break;
			case 112:
				type = EPipe_BotTom;
				break;
			case 201:
				type = ECloud;
				break;
			case 202:
				type = EFence;
				break;
			case 203:
				type = EGrass;
				break;
			case 204:
				type = EMoutain;
				break;
			case 2:
				type = ECoin;
				break;
			case 3:
				type = EQuestionBox_Flower;
				break;
			case 4:
				type = EFungi;
				break;
			case 5:
				type = EQuestionBox_Red;
				break;
			case 6:
				type = EQuestionBox_Green;
				break;
			case 7:
				type = EQuestionBox;
				break;
			case 8:
				type = ETurtle;
				break;
			case 9:
				type =  EBreakBrick;
				break;
			case 10:
				type = EFlowerMonster;
				break;
			case 11:
				type = EQuestionBox_Star;
				break;
			case 12:
				type = EGoal;
				break;
			default:
				type = ELand;
			}
			if(type!=ELand)
				mapGameObject.insert(pair<int,GameObject*>(id,Factory::getIns()->GameObjectMaker(D3DXVECTOR2( x , y),type)));
		}
	}
}

void CLoadMap::LoadToQuadtree(CQuadtree *quadtree)
{
	ifstream fileStream(_fileQuadtree);
	string line;
	char str[100];
	int worldWidth, worldHeight,idNode, x, y , w, h,id;
	char temp;
	fileStream>>worldWidth;
	getline(fileStream,line);
	if ( fileStream.is_open())
	{
		while ( !fileStream.eof() )
		{
			string::iterator str_i,str_j;
			vector<string> split; // Cắt chuỗi và đưa vào vector split
			getline(fileStream,line);
			if(line.size() > 0)
			{
				if(line.back()!=' ')
					line.push_back(' ');
				for (str_i = line.begin(); str_i < line.end(); )
				{
					str_j = find(str_i,line.end(),' ');
					if(str_i != str_j)
						split.push_back(string(str_i,str_j));
					str_i = str_j + 1;
				}
				idNode = atoi(split[0].c_str());
				x = atoi(split[1].c_str());
				y = atoi(split[2].c_str());
				w = atoi(split[3].c_str());
				h = w;
				QNode* node = new QNode(idNode,CRectMake(x,y,w,h));
				if(split.size() >=4)
				for (int i = 4; i< split.size();++i)
				{
					id = atoi(split[i].c_str());
					node->insertObject(mapGameObject[id]);
				}
				mapQuadtree.insert(pair<int,QNode*>(idNode,node));
			}
		}
	}
	//Duyet map de link quadtree
	map<int , QNode*>::iterator it;
	for (it = mapQuadtree.begin(); it!=mapQuadtree.end();it++)
	{
		if(it->first == 0) // Node goc
			quadtree->setRootNode(it->second);
		else  //ko phai root
		{
			int p = it->second->getParentID(); //id cua node cha
			int child = it->first%8; //
			QNode* pNode = mapQuadtree[p]; //con tro toi node cha
			switch(child)
			{
			case 1:
				pNode->_tl = it->second;
				break;
			case 2:
				pNode->_tr = it->second;
				break;
			case 3:
				pNode->_bl = it->second;
				break;
			case 4:
				pNode->_br = it->second;
				break;
			}
		}
	}
}

void CLoadMap::LoadGameObjectFromSaveFile()
{
	ifstream fileStream(_fileGameObject);
	int  width,id, typeObject;
	float x, y;
	int state;
	EObjectType type = EL_Brick;
	// read file
	
	fileStream>>width;
	mapWidth = width;
	if ( fileStream.is_open())
	{
		while ( !fileStream.eof() )
		{
			fileStream>>id;
			fileStream>>typeObject;
			fileStream>>x;
			fileStream>>y;
			fileStream>>state;

			switch (typeObject)
			{
			case 101:
				type = EL_Brick;
				break;
			case 102:
				type = EL_Block;
				break;
			case 103:
				type = EL_Land1;
				break;
			case 104:
				type = EL_Land2;
				break;
			case 105:
				type = EL_Land3;
				break;
			case 106:
				type = EL_PiPe;
				break;
			case 107:
				type = EL_BarDown;
				break;
			case 108:
				type = EL_BarUp;
				break;
			case 109:
				type = EPipe_Down;
				break;
			case 110:
				type = EPipe_Up;
				break;
			case 111:
				type = EPipe_Right;
				break;
			case 112:
				type = EPipe_BotTom;
				break;
			case 201:
				type = ECloud;
				break;
			case 202:
				type = EFence;
				break;
			case 203:
				type = EGrass;
				break;
			case 204:
				type = EMoutain;
				break;
			case 2:
				type = ECoin;
				break;
			case 3:
				type = EQuestionBox_Flower;
				break;
			case 4:
				type = EFungi;
				break;
			case 5:
				type = EQuestionBox_Red;
				break;
			case 6:
				type = EQuestionBox_Green;
				break;
			case 7:
				type = EQuestionBox;
				break;
			case 8:
				type = ETurtle;
				break;
			case 9:
				type =  EBreakBrick;
				break;
			case 10:
				type = EFlowerMonster;
				break;
			case 11:
				type = EQuestionBox_Star;
				break;
			case 12:
				type = EGoal;
				break;
			default:
				type = ELand;
			}
			if(type!=ELand)
				mapGameObject.insert(pair<int,GameObject*>(id,Factory::getIns()->GameObjectMaker(D3DXVECTOR2( x , y),type, (EState)state)));
		}
	}
}